﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DC2010.Objects.Data;
using DC2010;
using System.IO;
using DC2010_Editor.Properties;
using System.Drawing.Drawing2D;

namespace DC2010_Editor.Controls.Utils
{
    public partial class ItemGfxForm : Form
    {
        private ItemGfx _igfx { get; set; }
        public ItemGfxForm(ItemGfx igfx)
        {
            _igfx = igfx;
            InitializeComponent();

            SetControls();
        }

        private void SetControls()
        {
            if (_igfx != null)
            {

                IconTextBox.Text = _igfx.Icon;
                TextBoxOnFloor.Text = _igfx.Floor;
                TextBoxSide.Text = _igfx.Side;
                TextBoxTowards.Text = _igfx.Towards;
                TextBoxAway.Text = _igfx.Away;

                FillTextboxes(TextBox01, X01TextBox, Y01TextBox, _igfx.Item01);
                FillTextboxes(TextBox03, X03TextBox, Y03TextBox, _igfx.Item03);

                FillTextboxes(TextBox11, X11TextBox, Y11TextBox, _igfx.Item11);
                FillTextboxes(TextBox12, X12TextBox, Y12TextBox, _igfx.Item12);
                FillTextboxes(TextBox13, X13TextBox, Y13TextBox, _igfx.Item13);

                FillTextboxes(TextBox21, X21TextBox, Y21TextBox, _igfx.Item21);
                FillTextboxes(TextBox22, X22TextBox, Y22TextBox, _igfx.Item22);
                FillTextboxes(TextBox23, X23TextBox, Y23TextBox, _igfx.Item23);

                FillTextboxes(TextBox31, X31TextBox, Y31TextBox, _igfx.Item31);
                FillTextboxes(TextBox32, X32TextBox, Y32TextBox, _igfx.Item32);
                FillTextboxes(TextBox33, X33TextBox, Y33TextBox, _igfx.Item33);
            }
        }

        private void FillTextboxes(TextBox tb, TextBox tbx, TextBox tby, DungeonItem di)
        {
            if (di != null)
            {
                tb.Text = di.stex;
                //tbx.Text = di.offset.X.ToString();
                //tby.Text = di.offset.Y.ToString();
            }
        }


        private void GetControls()
        {
            if (_igfx != null)
            {
                _igfx.Icon = IconTextBox.Text;
                _igfx.Floor = TextBoxOnFloor.Text;
                _igfx.Side = TextBoxSide.Text;
                _igfx.Towards = TextBoxTowards.Text;
                _igfx.Away = TextBoxAway.Text;

                _igfx.Item03.stex = TextBox03.Text;
//                _igfx.Item03.offset = new Vector2(Tools.T2F(X03TextBox.Text), Tools.T2F(Y03TextBox.Text));
  //              _igfx.Item03.offsetAlt = new Vector2(Tools.T2F(X03TextBox.Text), Tools.T2F(Y03TextBox.Text));

                _igfx.Item01.stex = TextBox01.Text;
//                _igfx.Item01.offset = new Vector2(Tools.T2F(X01TextBox.Text), Tools.T2F(Y01TextBox.Text));
  //              _igfx.Item01.offsetAlt = new Vector2(Tools.T2F(X01TextBox.Text), Tools.T2F(Y01TextBox.Text));

                _igfx.Item11.stex = TextBox11.Text;
    //            _igfx.Item11.offset = new Vector2(Tools.T2F(X11TextBox.Text), Tools.T2F(Y11TextBox.Text));
      //          _igfx.Item11.offsetAlt = new Vector2(Tools.T2F(X11TextBox.Text), Tools.T2F(Y11TextBox.Text));

                _igfx.Item21.stex = TextBox21.Text;
        //        _igfx.Item21.offset = new Vector2(Tools.T2F(X21TextBox.Text), Tools.T2F(Y21TextBox.Text));
          //      _igfx.Item21.offsetAlt = new Vector2(Tools.T2F(X21TextBox.Text), Tools.T2F(Y21TextBox.Text));

                _igfx.Item31.stex = TextBox31.Text;
            //    _igfx.Item31.offset = new Vector2(Tools.T2F(X31TextBox.Text), Tools.T2F(Y31TextBox.Text));
              //  _igfx.Item31.offsetAlt = new Vector2(Tools.T2F(X31TextBox.Text), Tools.T2F(Y31TextBox.Text));

                _igfx.Item12.stex = TextBox12.Text;
      //          _igfx.Item12.offset = new Vector2(Tools.T2F(X12TextBox.Text), Tools.T2F(Y12TextBox.Text));
        //        _igfx.Item12.offsetAlt = new Vector2(Tools.T2F(X12TextBox.Text), Tools.T2F(Y12TextBox.Text));

                _igfx.Item22.stex = TextBox22.Text;
          //      _igfx.Item22.offset = new Vector2(Tools.T2F(X22TextBox.Text), Tools.T2F(Y22TextBox.Text));
            //    _igfx.Item22.offsetAlt = new Vector2(Tools.T2F(X22TextBox.Text), Tools.T2F(Y22TextBox.Text));

                _igfx.Item32.stex = TextBox32.Text;
              //  _igfx.Item32.offset = new Vector2(Tools.T2F(X32TextBox.Text), Tools.T2F(Y32TextBox.Text));
                //_igfx.Item32.offsetAlt = new Vector2(Tools.T2F(X32TextBox.Text), Tools.T2F(Y32TextBox.Text));

                _igfx.Item13.stex = TextBox13.Text;
    //            _igfx.Item13.offset = new Vector2(Tools.T2F(X13TextBox.Text), Tools.T2F(Y13TextBox.Text));
      //          _igfx.Item13.offsetAlt = new Vector2(Tools.T2F(X13TextBox.Text), Tools.T2F(Y13TextBox.Text));

                _igfx.Item23.stex = TextBox23.Text;
        //        _igfx.Item23.offset = new Vector2(Tools.T2F(X23TextBox.Text), Tools.T2F(Y23TextBox.Text));
          //      _igfx.Item23.offsetAlt = new Vector2(Tools.T2F(X23TextBox.Text), Tools.T2F(Y23TextBox.Text));

                _igfx.Item33.stex = TextBox33.Text;
            //    _igfx.Item33.offset = new Vector2(Tools.T2F(X33TextBox.Text), Tools.T2F(Y33TextBox.Text));
              //  _igfx.Item33.offsetAlt = new Vector2(Tools.T2F(X33TextBox.Text), Tools.T2F(Y33TextBox.Text));
            }
        }

        private void crystalButton3_Click(object sender, EventArgs e)
        {
            GetControls();
            Root.I.ItemSystem.SaveData();
            Root.I.ItemSystem.SaveInfo();
            Close();
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            GetControls();
            Root.I.ItemSystem.SaveData();
            Root.I.ItemSystem.SaveInfo();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            GetControls();
            pictureBox1.Refresh();
            pictureBox2.Refresh();
            pictureBox3.Refresh();

            FloorPB.Refresh();
            TowardsPB.Refresh();
            AwayPB.Refresh();
            SidePB.Refresh();
        }

        private Image GetImage(string partialPath)
        {
            string s = Environment.CurrentDirectory + "\\Gfx\\" + partialPath+".png";
            if (File.Exists(s))
            {
                return Image.FromFile(s);
            }

            return null;
        }

        public void DrawScaled(Graphics g, string texture, System.Drawing.Point origin, float scale)
        {
            //get what we want to draw
            Image img = GetImage(texture);

            // did we get something
            if (img != null)
            {
                Image i = Tools.ScaleByPercent(img, (int)(scale*100));

                if (scale == 1) Width1 = i.Width;
                if (scale == 0.7f) Width2 = i.Width;
                if (scale == 0.5f) Width3 = i.Width;

                if (i!=null)
                {
                    g.DrawImage(i, origin);
                }
            }
        }
        int Width1 = 0;
        int Width2 = 0;
        int Width3 = 0;

        private void pictureBox3_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx != null)
            {

                //draw 03
                if (_igfx.Item03 != null)
                {
                    Image img = GetImage(_igfx.Item03.stex);
                    if (img != null)
                        e.Graphics.DrawImage(img, new PointF(_igfx.Item03.offset.X, _igfx.Item03.offset.Y));
                }

                //draw 01
                if (_igfx.Item01 != null)
                {
                    Image img = GetImage(_igfx.Item01.stex);
                    if (img != null)
                        e.Graphics.DrawImage(img, new PointF(_igfx.Item01.offset.X, _igfx.Item01.offset.Y));
                }

                //draw 11
                if ((_igfx.Item11 != null)&&(radioButton1.Checked))
                {
                    DrawScaled(e.Graphics, _igfx.Item11.stex, new System.Drawing.Point((int)_igfx.Item11.offset.X, (int)_igfx.Item11.offset.Y), 1f);
                }

                //draw 12
                if ((_igfx.Item12 != null)&&(radioButton1.Checked))
                {
                    DrawScaled(e.Graphics, _igfx.Item12.stex, new System.Drawing.Point((int)_igfx.Item12.offset.X, (int)_igfx.Item12.offset.Y), 1f);
                }

                //draw 13
                if ((_igfx.Item13 != null)&&(radioButton1.Checked))
                {
                    DrawScaled(e.Graphics, _igfx.Item13.stex, new System.Drawing.Point((int)_igfx.Item13.offset.X, (int)_igfx.Item13.offset.Y), 1f);
                }

                //draw 21
                if ((_igfx.Item21 != null)&&(radioButton2.Checked))
                {
                    DrawScaled(e.Graphics, _igfx.Item21.stex, new System.Drawing.Point((int)_igfx.Item21.offset.X, (int)_igfx.Item21.offset.Y), 0.7f);
                }

                //draw 22
                if ((_igfx.Item22 != null)&&(radioButton2.Checked))
                {
                    DrawScaled(e.Graphics, _igfx.Item22.stex, new System.Drawing.Point((int)_igfx.Item22.offset.X, (int)_igfx.Item22.offset.Y), 0.7f);
                }

                //draw 23
                if ((_igfx.Item23 != null)&&(radioButton2.Checked))
                {
                    DrawScaled(e.Graphics, _igfx.Item23.stex, new System.Drawing.Point((int)_igfx.Item23.offset.X, (int)_igfx.Item23.offset.Y), 0.7f);
                }

                //draw 31
                if ((_igfx.Item31 != null)&&(radioButton3.Checked))
                {
                    DrawScaled(e.Graphics, _igfx.Item31.stex, new System.Drawing.Point((int)_igfx.Item31.offset.X, (int)_igfx.Item31.offset.Y), 0.5f);
                }

                //draw 32
                if ((_igfx.Item32 != null) && (radioButton3.Checked))
                {
                    DrawScaled(e.Graphics, _igfx.Item32.stex, new System.Drawing.Point((int)_igfx.Item32.offset.X, (int)_igfx.Item32.offset.Y), 0.5f);
                }

                //draw 33
                if ((_igfx.Item33 != null) && (radioButton3.Checked))
                {
                    DrawScaled(e.Graphics, _igfx.Item33.stex, new System.Drawing.Point((int)_igfx.Item33.offset.X, (int)_igfx.Item21.offset.Y), 0.5f);
                }
            }

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
                pictureBox3.Image = Resources.Template01;

            if (radioButton2.Checked)
                pictureBox3.Image = Resources.Template02;

            if (radioButton3.Checked)
                pictureBox3.Image = Resources.Template03;

            crystalButton2_Click(null, null);
        }

        private void crystalButton4_Click(object sender, EventArgs e)
        {
            if (TextBox31.Text.Contains("31"))
            {
                TextBox33.Text = TextBox31.Text.Replace("31", "33");
                TextBox23.Text = TextBox21.Text.Replace("21", "23");
                TextBox13.Text = TextBox11.Text.Replace("11", "13");
            }

            if (TextBox31.Text.Contains("13"))
            {
                TextBox33.Text = TextBox31.Text.Replace("13", "33");
                TextBox23.Text = TextBox21.Text.Replace("12", "32");
                TextBox13.Text = TextBox11.Text.Replace("11", "31");
            }

            Y33TextBox.Text = Y31TextBox.Text;
            Y23TextBox.Text = Y21TextBox.Text;
            Y13TextBox.Text = Y11TextBox.Text;

            int Width = Resources.Template01.Width;

            Image img33 = GetImage(TextBox33.Text);
            Image img23 = GetImage(TextBox23.Text);
            Image img13 = GetImage(TextBox13.Text);

            if (img13!=null) X13TextBox.Text = (Width-Tools.T2I(X11TextBox.Text)-Width1).ToString();
            if (img23 != null) X23TextBox.Text = (Width - Tools.T2I(X21TextBox.Text) - Width2).ToString();
            if (img33 != null) X33TextBox.Text = (Width - Tools.T2I(X31TextBox.Text) - Width3).ToString();
        }

        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx != null)
            {
                if (_igfx.Icon != null)
                {
                    Image img = GetImage(_igfx.Icon);
                    if (img != null) e.Graphics.DrawImage(img, new PointF(0,0));
                }
            }
        }

        private void FloorPB_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Floor != null)
            {
                Image img = GetImage(_igfx.Floor);
                if (img != null)
                {
                    int width = img.Width;
                    int height = img.Height;
                    if (height > AwayPB.Height)
                    {
                        float percent = (float)AwayPB.Height / 100;

                        int newHeight = (int)((float)height * percent);
                        int newWidth = (int)((float)width * percent);
                        e.Graphics.DrawImage(img, 0, 0, newWidth, newHeight);
                    }
                    else
                    {
                        e.Graphics.DrawImage(img, 0, 0);
                    }
                }
            }
        }

        private void SidePB_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Side != null)
            {
                Image img = GetImage(_igfx.Side);
                if (img != null)
                {
                    int width = img.Width;
                    int height = img.Height;
                    if (height > AwayPB.Height)
                    {
                        float percent = (float)AwayPB.Height / 100;

                        int newHeight = (int)((float)height * percent);
                        int newWidth = (int)((float)width * percent);
                        e.Graphics.DrawImage(img, 0, 0, newWidth, newHeight);
                    }
                    else
                    {
                        e.Graphics.DrawImage(img, 0, 0);
                    }
                }
            }
        }

        private void TowardsPB_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Towards != null)
            {
                Image img = GetImage(_igfx.Towards);
                if (img != null)
                {
                    int width = img.Width;
                    int height = img.Height;
                    if (height > AwayPB.Height)
                    {
                        float percent = (float)AwayPB.Height / 100;

                        int newHeight = (int)((float)height * percent);
                        int newWidth = (int)((float)width * percent);
                        e.Graphics.DrawImage(img, 0, 0, newWidth, newHeight);
                    }
                    else
                    {
                        e.Graphics.DrawImage(img, 0, 0);
                    }
                }
            }

        }

        private void AwayPB_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Away != null)
            {
                Image img = GetImage(_igfx.Away);
                if (img != null)
                {
                    int width = img.Width;
                    int height = img.Height;
                    if (height > AwayPB.Height)
                    {
                        float percent = (float)AwayPB.Height / 100;

                        int newHeight = (int)((float)height * percent);
                        int newWidth = (int)((float)width * percent);
                        e.Graphics.DrawImage(img, 0, 0, newWidth, newHeight);
                    }
                    else
                    {
                        e.Graphics.DrawImage(img, 0, 0);
                    }
                }
            }

        }
    
    }
}
